package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.kh.KHDBConfig
import cn.com.kh.KHUtil
import org.json.JSONArray
import org.json.JSONObject
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import cn.com.kh.HttpClientProxy;

/**
 * 同步档位系统人员架构信息至工单系统
 */

class autoGetUser implements CallBackInvoke {
    private static final Logger log = LoggerFactory.getLogger(autoGetUser.class);

    public void doCallBack(EliteBean eliteBean, String callBack, Map map, String paramString) {

        def httpURL = KHDBConfig.getAPIURL(eliteBean,"getUserList");

        def in_marketId=KHDBConfig.getParam(eliteBean,"MARKID");

        def sendBody1 = "{\"marketId\":\""+in_marketId+"\",\"page\":1,\"size\":500}";
        def result1 = HttpClientProxy.sendPost(httpURL, sendBody1, "", 0);

        JSONObject JsonUser1 = new JSONObject(result1);
        //log.info("[获取人员信息结果]:" + JsonUser1);
        int re_status1 = JsonUser1.getInt("status");
        if (re_status1 == 200) {
            int pages = JsonUser1.getJSONObject("data").getInt("pages");
            int nextPage = 1;
            for (int j = 1; j <= pages; j++) {
                def sendBody = "{\"marketId\":\""+in_marketId+"\",\"page\":" + nextPage + ",\"size\":500}";

                def result = HttpClientProxy.sendPost(httpURL, sendBody, "", 0);
                JSONObject JsonUser = new JSONObject(result);
                int re_status = JsonUser.getInt("status");
                if (re_status == 200) {
                    nextPage = JsonUser1.getJSONObject("data").getInt("nextPage");
                    JSONObject obj = JsonUser.getJSONObject("data");
                    JSONArray jsonArray = obj.getJSONArray("list");
                    int dataArrayLen = jsonArray.length();
                    JSONObject dataObject = null;
                    for (int i = 0; i < dataArrayLen; i++) {
                        dataObject = jsonArray.getJSONObject(i);
                        def id = KHUtil.GetJV(dataObject, "id");//员工编号
                        def username = KHUtil.GetJV(dataObject, "username");//员工姓名
                        def mobile = KHUtil.GetJV(dataObject, "mobile");//电话号码
                        def officeCode = KHUtil.GetJV(dataObject, "officeCode");//归属部门编号
                        def in_status = KHUtil.GetJV(dataObject, "status");//账号状态
                        def email = KHUtil.GetJV(dataObject, "email");//邮箱
                        def loginCode = KHUtil.GetJV(dataObject, "loginCode");//登录账号
                        def postCode = KHUtil.GetJV(dataObject, "postCode");//档位系统岗位，同步至工单系统角色表
                        def postName = KHUtil.GetJV(dataObject, "postName");//档位系统岗位名称

                        def status = "";
                        def rolegroup_id = "";
                        def staff_id = "";
                        def role_id = "";
                        def ram_id = KHUtil.getRandomString(6).toUpperCase();//随机id

                        if (in_status == "0") {
                            status = "ONLINE";
                        } else {
                            status = "QUIT";
                        }

                        def selectSql1 = "select STAFF_ID from staff where dw_staff_id='" + id + "'";
                        List sqlResult = eliteBean.chaxun_sql(selectSql1, 'dataSource');
                        if (sqlResult.size() > 0) {
                            staff_id = sqlResult.get(0).get("STAFF_ID");
                        }

                        def selectSql3 = "select ROLEGROUP_ID from ROLEGROUP where DW_ROLEGROUP_ID='" + officeCode + "'";
                        List sqlResult3 = eliteBean.chaxun_sql(selectSql3, 'dataSource');
                        log.debug(sqlResult3.toString());
                        if (sqlResult3.size() > 0) {
                            rolegroup_id = sqlResult3.get(0).get("ROLEGROUP_ID");
                        }

                        def selectSql4 = "select ROLE_ID from ROLE where dw_post_id='" + postCode + "'";
                        List sqlResult4 = eliteBean.chaxun_sql(selectSql4, 'dataSource');
                        log.debug(sqlResult4.toString());
                        if (sqlResult4.size() > 0) {
                            role_id = sqlResult4.get(0).get("ROLE_ID");
                        }

                        if (KHUtil.isNull(staff_id)) {
                            log.debug("未查询到"+staff_id+"，执行新增....");

                            String insertSql1 = "insert into staff(staff_id,staffname,mobile,defaultrolegroup,workstatus,e_mail,loginname,DW_STAFF_ID,DW_POSTCODE) values('" + ram_id + "','" + username + "','" + mobile + "','" + rolegroup_id + "','" + status + "','" + email + "','" + loginCode + "','" + id + "','" + postCode + "')";
                            def dataresult = eliteBean.update(insertSql1, 'dataSource');
                            log.debug("执行staff数据新增SQL结果:"+ ram_id +"|"+ dataresult.toString());

                            String insertSql2 = "INSERT INTO staffrolegroup(STAFF_ID, ROLEGROUP_ID) VALUES ('" + ram_id + "', '" + rolegroup_id + "')";
                            def dataresult2 = eliteBean.update(insertSql2, 'dataSource');
                            log.debug("执行staffrolegroup数据新增SQL结果:"+ ram_id +"|"+ dataresult2);

                            String insertSql3="insert into staffrole(STAFFROLE_GUID, ROLE_ID, STAFF_ID) VALUES (uuid(),'" + role_id + "','" + ram_id + "')";
                            def dataresult4 = eliteBean.update(insertSql3, 'dataSource');
                            log.debug("执行staffrole数据新增SQL结果:"+ postCode +"|"+ dataresult4);

                        } else {
                            log.debug("查询到已有"+staff_id+"，执行更新staff数据....");

                            String updateSql1 = "update staff set staffname='" + username + "',mobile='" + mobile + "',defaultrolegroup='" + rolegroup_id + "',workstatus='" + status + "',e_mail='" + email + "',loginname='" + loginCode + "',DW_POSTCODE='" + postCode + "' where STAFF_ID='" + staff_id + "' ";
                            def dataresult3 = eliteBean.update(updateSql1, 'dataSource');
                            log.debug("更新staff数据结果:" + staff_id + "|" + dataresult3);


                            log.debug("查询到已有"+staff_id+"，执行更新staffrolegroup数据....");

                            def updateSql3 = "delete from staffrolegroup where staff_id='" + staff_id + "'";
                            def dataresult4 = eliteBean.update(updateSql3, 'dataSource');
                            log.debug("删除staffrolegroup:" + staff_id + "|" + dataresult4);

                            def updateSql2 = "INSERT INTO staffrolegroup(STAFF_ID, ROLEGROUP_ID) VALUES ('" + staff_id + "', '" + rolegroup_id + "')";
                            def dataresult5 = eliteBean.update(updateSql2, 'dataSource');
                            log.debug("更新staffrolegroup数据结果:" + staff_id + "|" + dataresult5);


                            log.debug("查询到已有"+staff_id+"，执行更新staffrole数据....");

                            def u_staffrolegroup_sql = "delete from staffrole where staff_id='" + staff_id + "'";
                            def u_staffrolegroup_result = eliteBean.update(u_staffrolegroup_sql, 'dataSource');
                            log.debug("删除staffrole:" + postCode + "|" + u_staffrolegroup_result);

                            String insertSql4="insert into staffrole(STAFFROLE_GUID, ROLE_ID, STAFF_ID) VALUES (uuid(),'" + role_id + "','" + staff_id + "')";
                            def dataresult6 = eliteBean.update(insertSql4, 'dataSource');
                            log.debug("执行staffrole数据新增SQL结果:" + dataresult6);

                        }

                    }
                }
            }
        }
        return null;
    }
}
